<style>
    .viewNav {
        width: 160px;
        height: 36px;
        line-height: 36px;
        background: #FFFFFF;
    }

    .viewNav:after, .viewNavBox:after {
        content: '';
        display: block;
        clear: both;
    }

    .viewNav div {
        text-align: center;
        float: left;
        cursor: pointer;
        width: 79px;
        height: 36px;
        box-sizing: border-box;
        border: 1px solid #CCCCCC;
        font-size: 14px;
        font-family: Microsoft YaHei-Regular, Microsoft YaHei;
        font-weight: 400;
        color: #999999;
    }

    .viewNav div img {
        width: 20px;
        height: 20px;
        vertical-align: text-top;
    }

    .viewNav .viewNavList {
        border-right: none;
        border-radius: 5px 0 0 5px;
    }

    .viewNav .viewNavGraphics {
        border-radius: 0px 5px 5px 0px;
        border-left: none;
    }

    .viewNav > span {
        float: left;
        width: 1px;
        height: 36px;
        background-color: #0A8B0A;
    }

    .viewNav .active {
        color: #0A8B0A;
        border-color: #0A8B0A;
    }

    .viewNavMain {
        display: none;
        min-width: 1602px;
        overflow: hidden;
    }


    .viewNavBoxList {
        box-sizing: border-box;
        width: 210px;
        height: 270px;
        float: left;
        border: 1px solid #EDEDED;
        border-radius: 10px;
        margin-right: 20px;
        margin-bottom: 20px;
        position: relative;
        cursor: pointer;
        font-family: Microsoft YaHei-Regular, Microsoft YaHei;
    }

    .viewNavBoxList:nth-child(7n) {
        margin-right: 0;
    }

    .viewNavBoxList .viewNavBoxSpaceNum {
        width: 72px;
        height: 32px;
        box-sizing: border-box;
        position: absolute;
        left: 0;
        top: 8px;
        background-image: url('assets/images/spaceNum.png');
        line-height: 32px;
        text-align: center;
    }

    .viewNavBoxList .viewNavBoxListImg {
        margin: 20px 35px 0;
        width: 140px;
        height: 140px;
    }

    .viewNavBoxListImg img {
        width: 100%;
        height: 100%;
    }

    .viewNavBoxList .viewNavBoxListTitle {
        box-sizing: border-box;
        padding: 0 10px;
        margin-top: 14px;
        font-size: 16px;
        font-weight: bold;
        color: #333333;
        line-height: 19px;
        text-align: center;
        height: 40px;
        width: 100%;
        overflow: hidden;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        text-overflow: ellipsis;
    }

    .viewNavBoxList .viewNavBoxListBottom {
        position: absolute;
        width: 100%;
        left: 0;
        bottom: 0;
        height: 40px;
        background: #FAFBFF;
        border-radius: 0 0 10px 10px;
        padding: 0 14px;
        box-sizing: border-box;
    }

    .viewNavBoxListBottom > span {
        box-sizing: border-box;
        text-align: center;
        line-height: 40px;
        font-size: 12px;
        font-weight: 400;
        color: #333333;
        float: left;
    }

    .viewNavBoxListBottom > span:last-child {
        float: right;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header" id="exportFrom">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt" style="width: calc(82% - 160px)">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">主柜：</label>
                                    <div class="layui-input-inline">
                                        <select  name="cabCode" lay-filter="cabCode"  lay-search lay-verify="">
                                            <option value="">请选主柜</option>
                                        </select>
                                    </div>
                                </div>
<!--                                <div class="layui-inline">-->
<!--                                    <label class="layui-form-label w-auto">货道编号：</label>-->
<!--                                    <div class="layui-input-inline">-->
<!--                                        <input name="spaceCode" class="layui-input" type="text" placeholder="请输入货道编号"/>-->
<!--                                    </div>-->
<!--                                </div>-->
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">物料编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">规格码：</label>
                                    <div class="layui-input-inline">
                                        <input name="oldSpecCode" class="layui-input" type="text" placeholder="请输入规格码"/>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <div class="viewNav" id="viewNav">
                                        <div class="viewNavList active"><img src="assets/images/navlist1.png">列表</div>
                                        <span></span>
                                        <div class="viewNavGraphics"><img src="assets/images/navgraphics.png">图文</div>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                    <button id="cellInventoryCabinetExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="storageInventoryInoutTable" lay-filter="storageInventoryInoutTable"></table>
            <div class="viewNavMain" id="viewNavMain">
                <div class="viewNavBox" style="" id="viewNavBox"></div>
                <div id="viewNavPage" style="text-align: right;"></div>
            </div>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="storageInventoryInoutTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="storageInventoryBtn">库存分布</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="storageInventoryInoutForm">
    <div class="layui-fluid">
        <table class="layui-table" id="inventoryProductTable" lay-filter="inventoryProductTable"></table>
    </div>
</script>

<script type="text/html" id="inventorySpecForm">
    <div class="layui-fluid">
        <table class="layui-table" id="inventorySpecTable" lay-filter="inventorySpecTable"></table>
    </div>
</script>

<script type="text/html" id="inventoryProductTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="inventorySpecBtn">库存分布</a>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin','laypage'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        let laypage = layui.laypage;
        let viewNavPageCount = 0, listText = 0; //viewNavPageCount(图文状态下的页码)  listText(区分图文视图1 和 列表视图0)
        let viewNavBoxList = new Map();

        // 渲染表格
        var insTb = table.render({
            elem: '#storageInventoryInoutTable',
            url: config.base + 'enterprisecab/wcabinet/admin/wcabCellInventoryAlarm/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'}
                ,{field: 'belongCabinet', sort: false, title: '主柜编码'}
                ,{field: 'cabName', sort: false, title: '称重柜名称'}
                ,{field: 'cabCode', sort: false, title: '称重柜编码'}
                ,{field: 'skuName', sort: false, title: '物料名称'}
                ,{field: 'productBrandName', sort: false, title: '品牌'}
                ,{field: 'skuCode', sort: false, title: '物料编号'}
                ,{field: 'skuSpecOld', sort: false, title: '规格编号'}
                ,{field: 'quantity', sort: false, title: '库存数量'}
                ,{field: 'quantitySetting', sort: false, title: '告警库存数量'}
                ,{field: 'unitName', sort: false, title: '单位'}
                ,{align: 'center', toolbar: '#storageInventoryInoutTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
            }
        });

        // 工具条点击事件
        table.on('tool(storageInventoryInoutTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'storageInventoryBtn') { //修改
                showStorageInventoryModel(data);
            }
        });

        //监听排序
        table.on('sort(storageInventoryInoutTable)', function(obj){
            table.reload('storageInventoryInoutTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });
        $('#viewNav .viewNavGraphics ').click(function () {
            listText = 1;
            $(this).addClass('active').siblings().removeClass('active');
            $(this).find('img').attr('src', 'assets/images/navgraphics1.png');
            $(this).siblings('div').eq(0).find('img').attr('src', 'assets/images/navlist.png');
            $('[lay-id="storageInventoryInoutTable"]').hide();
            $('#viewNavMain').show();
        });

        $('#viewNav .viewNavList ').click(function () {
            listText = 0;
            $('[lay-id="storageInventoryInoutTable"]').show();
            $('#viewNavMain').hide();
            $(this).addClass('active').siblings().removeClass('active');
            $(this).find('img').attr('src', 'assets/images/navlist1.png');
            $(this).siblings('div').eq(0).find('img').attr('src', 'assets/images/navgraphics.png');
        });

        function viewNavBox(data) {
            layer.load(2);
            let html = '';
            if (!data || data.length <= 0) {
                $('#viewNavBox').html(html);
                $('#viewNavPage').hide();
            }else {
                for (let i = 0; i < data.length; i++) {
                    viewNavBoxList.set(data[i].id, data[i]);
                    html += `<div class="viewNavBoxList" id="${data[i].id}">
                            <div class="viewNavBoxListImg"><img src="${data[i].skuPic}" alt=""></div>
                            <div class="viewNavBoxListTitle">${data[i].skuSpec}</div>
                            <div class="viewNavBoxListBottom"><span>${data[i].skuCode}</span><span>${data[i].quantity}</span></div>
                        </div>`
                }
                $('#viewNavPage').show();
                $('#viewNavBox').html(html);
            }
            layer.closeAll();
        }
        admin.reqSync('enterprisecab/wcabinet/admin/wcabCellInventoryAlarm/list', {page: 1, limit: 21}, res => {
            if (res.code === 200) {
                viewNavPageCount = res.count;
                viewNavBox(res.data);
                viewNavPageFn()
            }
        }, 'get');

        function viewNavPageFn(_condition) {
            laypage.render({
                elem: 'viewNavPage' //注意，这里的 test1 是 ID，不用加 # 号
                , count: viewNavPageCount //数据总数，从服务端得到
                , limit: 21
                , jump: function (obj, first) {
                    if (!first) {
                        let condition = _condition ? $.extend(_condition, {
                            page: obj.curr,
                            limit: obj.limit
                        }) : {page: obj.curr, limit: obj.limit};
                        admin.req('enterprisecab/wcabinet/admin/wcabCellInventoryAlarm/list', condition, res => {
                            if (res.code === 200) {
                                viewNavBox(res.data);
                            }
                        }, 'get');
                    }
                }
            });
        }

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            if (listText == 0) {
                insTb.reload({where: data.field,page: {curr: 1}},'data');
            } else {
                admin.reqSync('enterprisecab/wcabinet/admin/wcabCellInventoryAlarm/list', $.extend(data.field, {
                    page: 1,
                    limit: 21
                }), res => {
                    if (res.code === 200) {
                        viewNavPageCount = res.count;
                        viewNavBox(res.data);
                        viewNavPageFn(data.field)
                    }
                }, 'get');
            }
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            var field = data.field;
            for(let key in field){
                field[key] = ''
            }
            if (listText == 0) {
                insTb.reload({where: field,page: {curr: 1}},'data');
            }else {
                admin.reqSync('enterprisecab/wcabinet/admin/wcabCellInventoryAlarm/list', {page: 1,limit: 21}, res => {
                    if (res.code === 200) {
                        viewNavPageCount = res.count;
                        viewNavBox(res.data);
                        viewNavPageFn(field)
                    }
                }, 'get');
            }
        });

        admin.req('enterprisecab/cabinet/admin/cabinet/list', {limit:999,cabinetNature:'main',cabinetType:'gravity'}, function (res) {
            if (200 == res.code) {
                var optionHtml='<option value=""> 请选择柜子</option>';
                for(var k=0;k<res.data.length;k++){
                    optionHtml+='<option value="'+res.data[k].cabCode+'">'+res.data[k].cabCode+'</option>';
                }
                $("select[name='cabCode']").html(optionHtml);
                form.render('select')
            } else {
                layer.msg(res.msg, {icon: 2, time: 1000});
            }
        }, 'GET');

        form.render('select');

        // 显示库存分布弹窗
        function showStorageInventoryModel(data) {
            admin.open({
                type: 1,
                area: '1270px',
                offset: '65px',
                title: '库存分布',
                content: $('#storageInventoryInoutForm').html(),
                success: function () {
                    getInventoryProduct(data);
                }
            });
        }

        function getInventoryProduct(data){
            var inventoryProductTb = table.render({
                elem: '#inventoryProductTable',
                url: config.base + 'enterprisecab/wcabinet/admin/wcabCellSingleInventoryProduct/getListInventoryBySkuCode?skuCode='+data.skuCode+'&belongCabinet='+data.belongCabinet,
                page: false,
                cellMinWidth: 100,
                cols: [[
                    {type: 'numbers'}
                    ,{field: 'cabCode', sort: false, title: '柜子编号'}
                    ,{field: 'cabName', title: '企业自定义名称'}
                    ,{field: 'productName', sort: false, title: '物料名称'}
                    ,{field: 'skuCode', sort: false, title: '物料编号'}
                    ,{field: 'skuSpecOld', sort: false, title: '规格编号'}
                    ,{field: 'weight', sort: false, title: '库存重量'}
                    ,{field: 'unitName', sort: false, title: '单位'}
                    , {align: 'center', toolbar: '#inventoryProductTableBar', title: '操作', width: 200}
                ]]
            });
        }

        // 工具条点击事件
        table.on('tool(inventoryProductTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'inventorySpecBtn') { //修改
                showInventorySpecModel(data);
            }
        });

        function showInventorySpecModel(data){
            admin.open({
                type: 1,
                area: '1280px',
                offset: '65px',
                title: '库存分布',
                content: $('#inventorySpecForm').html(),
                success: function () {
                    getInventorySpec(data);
                }
            });
        }

        function getInventorySpec(data){
            var inventoryProductTb = table.render({
                elem: '#inventorySpecTable',
                url: config.base + 'enterprisecab/wcabinet/admin/wcabCellInventorySpace/list?cabCode='+data.cabCode+'&skuCode='+data.skuCode,
                page: true,
                cellMinWidth: 100,
                cols: [[
                    {type: 'numbers'}
                    ,{field: 'spaceCode', sort: false, title: '货道编号'}
                    ,{field: 'cabCode', sort: false, title: '柜子编号'}
                    ,{field: 'cabName', title: '企业自定义名称'}
                    ,{field: 'skuName', sort: false, title: '物料名称'}
                    ,{field: 'skuCode', sort: false, title: '物料编号'}
                    ,{field: 'skuSpecOld', sort: false, title: '规格编号'}
                    ,{field: 'weight', sort: false, title: '库存重量'}
                    // ,{field: 'quantityMax', sort: false, title: '最大库存'}
                    ,{field: 'unitName', sort: false, title: '单位'}
                ]],
            });
        }


        // 导出
       /* $('#cellInventoryCabinetExport').click(function () {
            location.href = config.base + 'enterprisecab/cabinet/admin/cellInventoryCabinet/export';
        });*/
        $('#cellInventoryCabinetExport').click(function () {
            layer.load(2);
            let d = {page: 1, limit: 9999999}, t = layui.$('#exportFrom [name]').serializeArray();
            layui.$.each(t, function () {
                d[this.name] = this.value;
            });

            admin.req('enterprisecab/wcabinet/admin/wcabCellInventoryAlarm/list', d, res => {
                layer.closeAll('loading');
                if (res.code === 200) {
                    table.exportFile(insTb.config.id, res.data, 'xls', '库存告警提示');
                } else {
                    layer.msg('导出失败', {icon: 2})
                }
            });

        });

    });
</script>